package com.ht.shortlink.admin.remote.dto;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ht.shortlink.admin.common.convention.result.Result;
import com.ht.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.ht.shortlink.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.ht.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.ht.shortlink.admin.remote.dto.resp.ShortLinkGroupCountRespDTO;
import com.ht.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface ShortLinkRemoteService {
    default Result<ShortLinkCreateRespDTO> create(@RequestBody ShortLinkCreateReqDTO requestParam){
        String resultStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSONUtil.toJsonStr(requestParam));
        return JSON.parseObject(resultStr, new TypeReference<>(){});
    }

    default Result<IPage<ShortLinkPageRespDTO>> queryPage(ShortLinkPageReqDTO requestParam){
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("gid", requestParam.getGid());
        reqMap.put("current", requestParam.getCurrent());
        reqMap.put("size", requestParam.getSize());
        String resultStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", reqMap);
        return JSON.parseObject(resultStr, new TypeReference<>(){});
    }

    default Result<List<ShortLinkGroupCountRespDTO>> listGroupLinkCount(@RequestParam("requestParams") List<String> requestParam){
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("requestParams", requestParam);
        String resultStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", reqMap);
        return JSON.parseObject(resultStr, new TypeReference<>(){});
    }
}
